package com.itttq.kb.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.itttq.kb.model.po.TopicPo;
import com.itttq.kb.service.TopicCategoryService;
import com.itttq.kb.service.TopicReplyService;
import com.itttq.kb.utils.Constants;

@RestController
@RequestMapping("")
public class MainController extends GenericController implements CommandLineRunner {
	private static final long serialVersionUID = -42906748724186843L;
	@Autowired
	private TopicReplyService topicReplyService;
	@Autowired
	private TopicCategoryService topicCategoryService;
	
	@RequestMapping(value={"","index.html"})
	public ModelAndView indexPage(HttpServletRequest request) {
		ModelAndView mv = gotoPage("index");
		mv = getUserAndTopic(null, mv, request);
		
		// 获取最新帖子
		mv.addObject("latestTopics", topicService.getLatestTopics(null, 10));
		// 获取置顶帖子
		TopicPo topic = new TopicPo();
		topic.setIsTop(Constants.STATUTS_OPEN);
		mv.addObject("topTopics", topicService.getTopics(topic, this.getPageParam(10)));
		return mv;
	}
	
	@RequestMapping("/{page}.html")
	public ModelAndView gotoPage(@PathVariable("page") String page) {
		return new ModelAndView(page);
	}
	
	@RequestMapping("/{dir}/{page}.html")
	public ModelAndView gotoPage(@PathVariable("dir") String dir, @PathVariable("page") String page) {
		return new ModelAndView(dir + Constants.SEPARATOR_BACKSLASH + page);
	}
	
	@Override
    public void run(String... args) throws Exception {
//		PageParamTo ppt = this.getPageParam(25);
//		TopicCategoryPo tc = new TopicCategoryPo();
//		tc.setParentId(0); // 获取一级分类
//		catchDataTo.setFirstTopicCategory(topicCategoryService.getTopicCategorys(tc, ppt));
    }
}
